﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;


namespace MDLoader
{

    public partial class Login : Form
    {
        private MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
        private MySqlConnection connection;
        public Login()
        {

            
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
           
            //登录
            string userName = this.textBox1.Text;
            string userPassword = this.textBox2.Text;
            if (userName.Equals("") || userPassword.Equals(""))
            {
                MessageBox.Show("用户名或密码不能为空！");
            }
            else
            {
                string strcon = "data source=localhost;database=md;user id=root;password=123456;pooling=true;charset=utf8;";
                MySqlConnection con = new MySqlConnection(strcon);
                try
                {
                    //2. 打开数据库
                    con.Open();
                    //3. sql语句
                    string sqlSel = "select count(*) from user where account = '" + userName + "' and password = '" + userPassword + "'";
                    MySqlCommand com = new MySqlCommand(sqlSel, con);
                    //4.判断executeScalar方法返回的参数是否大于0，大于0表示查找有数据
                    if (Convert.ToInt32(com.ExecuteScalar()) > 0)
                    {
                        MessageBox.Show("登录成功！");
                        Form1.gStr.MyName = userName;
                        //跳转主界面
                        this.DialogResult = DialogResult.OK;
                        this.Dispose();
                        this.Close();
                    }

                    //用户名和密码验证错误，提示错误。
                    else
                    {
                        MessageBox.Show("用户名或密码错误！");
                    }
                }
                catch (Exception ex)
                {
                    
                      MessageBox.Show(ex.Message.ToString() + "打开数据库失败");
                    

                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string userName = this.textBox1.Text;
            string userPassword = this.textBox2.Text;
            if (userName.Equals("") || userPassword.Equals(""))
            {
                MessageBox.Show("用户名或密码不能为空！");
            }
            else
            {
                string strcon = "data source=localhost;database=md;user id=root;password=123456;pooling=true;charset=utf8;";
                MySqlConnection con = new MySqlConnection(strcon);
                //2. 打开数据库
                con.Open();
                try
                {
                    
                    //3. sql语句
                    string sqlSel = "select count(*) from user where account = '" + userName  + "'";
                    MySqlCommand com = new MySqlCommand(sqlSel, con);
                    //4.判断executeScalar方法返回的参数是否大于0，大于0表示查找有数据
                    if (Convert.ToInt32(com.ExecuteScalar()) > 0)
                    {
                        MessageBox.Show("用户名已存在，请重新填写用户名");

                        
                    }

                    
                    else
                    {

                        sqlSel = "INSERT INTO user(account,password) VALUES('" + userName + "','" + userPassword + "')";
                        com = new MySqlCommand(sqlSel, con);
                        com.ExecuteNonQuery();
                        MessageBox.Show("注册成功，已登陆");
                        Form1.gStr.MyName = userName;
                        //跳转主界面
                        this.DialogResult = DialogResult.OK;
                        this.Dispose();
                        this.Close();
                    }
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message.ToString() + "打开数据库失败");


                }

            }

        }
    }
}
